<HTML><HEAD><TITLE>+Condition -> +Then ; +Else</TITLE>
</HEAD><BODY>[ <A HREF="index.html">Control</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>+Condition -> +Then ; +Else</H1>
Conditional construct - succeeds if Then succeeds for the first solution of Condition,
or if Condition fails and Else succeeds.
<DL>
<DT><EM>Condition</EM></DT>
<DD>Atom or compound term.
</DD>
<DT><EM>Then</EM></DT>
<DD>Atom or compound term.
</DD>
<DT><EM>Else</EM></DT>
<DD>Atom or compound term.
</DD>
</DL>
<H2>Description</H2>

   The conditional (if-then-else) construct.  First Condition is called
   and if this succeeds any further solutions of Condition are cut and
   Then is called.  Else is never executed in this case regardless of the
   outcome of Then.
<P>
   If Condition fails, Else is called.  In this case, Then is never executed.
<P>
   It is allowed, but not recommended to use -&gt;/2 without ;/2 as
   <PRE>
   ( Condition -&gt; Then )
   </PRE>
   because this is equivalent to
   <PRE>
   ( Condition -&gt; Then ; fail )
   </PRE>
   which is sometimes considered unintuitive. If this behaviour is really
   wanted, it can be expressed more clearly by
   <PRE>
   once Condition , Then
   </PRE>
<P>
   The more common idiom, where nothing is to be done in the else-case,
   must be written like this, using true/0:
   <PRE>
   ( Condition -&gt; Then ; true )
   </PRE>
<P>
   Also note that a !/0 inside Condition only has a local effect in Condition.
   If a !/0 appears in Then or Else, it cuts through the whole construct.

<P>
   Since -&gt;/2 and ;/2 have a lower precedence than ,/2, the whole construct
   should always be enclosed in parentheses:
    <PRE>
    ( Condition -&gt;
        Then
    ;
        Else
    )
    </PRE>

<H3>Modules</H3>
This predicate is sensitive to its module context (tool predicate, see @/2).
<H3>Fail Conditions</H3>
Fails if Condition succeeds and Then fails, or if Condition and Else fail
<H3>Resatisfiable</H3>
Resatisfiable if Condition succeeds and Then is resatisfiable, or Condition fails and Else is resatisfiable
<H3>Exceptions</H3>
<DL>
<DT><EM>(4) instantiation fault </EM>
<DD>One of the arguments is not instantiated.
<DT><EM>(5) type error </EM>
<DD>One of the arguments is neither an atom nor a compound term.
</DL>
<H2>Examples</H2>
<PRE>
Success:
      % Then-branch executed
      ?- X = 1, ( X == 1 -&gt; write(a) ; write(b) ).
      a
      X = 1
      Yes (0.00s cpu)

      % Else-branch executed
      ?- X = 2, ( X == 1 -&gt; write(a) ; write(b) ).
      b
      No (0.00s cpu)

      % the Condition is cut, the Then-branch isn't
      ?- ( member(X,[1,2]) -&gt; member(Y,[a,b]) ; member(Y,[c,d]) ).
      X = 1
      Y = a
      Yes (0.00s cpu, solution 1, maybe more)
      X = 1
      Y = b
      Yes (0.03s cpu, solution 2)
</PRE>
<H2>See Also</H2>
<A HREF="../../kernel/control/O-2.html">; / 2</A>, <A HREF="../../kernel/control/I-0.html">! / 0</A>, <A HREF="../../kernel/control/X-G-2.html">*-> / 2</A>
</BODY></HTML>
